﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICCriteria
    {
        public DataSet CCriteria_GetByCandidate(int CandidateID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCreteria_GetByCandidateID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("CandidateID", CandidateID);
            try
            {
                dbConn.Open();
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CCriteria_Delete(int CandidateID, int RecruitmentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCriteria_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CCriteria_CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CCriteria_RecruitmentID", RecruitmentID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public CCriteriaInfo CCriteria_GetInfo(int _CCriteria_CandidateID, string _CCriteria_Name, int _CCriteria_Ordinal, int _CCriteria_RecruitmentID)
        {
            CCriteriaInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCriteria_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CCriteria_CandidateID", _CCriteria_CandidateID);
            dbCmd.Parameters.AddWithValue("@CCriteria_Name", _CCriteria_Name);
            dbCmd.Parameters.AddWithValue("@CCriteria_Ordinal", _CCriteria_Ordinal);
            dbCmd.Parameters.AddWithValue("@CCriteria_RecruitmentID", _CCriteria_RecruitmentID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CCriteriaInfo();
                    retVal.ID = Convert.ToInt32(dr["CCriteria_ID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CCriteria_CandidateID"]);
                    retVal.Name = Convert.ToString(dr["CCriteria_Name"]);
                    retVal.Point = Convert.ToString(dr["CCriteria_Point"]);
                    retVal.Comments = Convert.ToString(dr["CCriteria_Comments"]);
                    retVal.Ordinal = Convert.ToInt32(dr["CCriteria_Ordinal"]);
                    retVal.RecruitmentID = Convert.ToInt32(dr["CCriteria_RecruitmentID"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public int CCriteria_Insert(CCriteriaInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCriteria_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CCriteria_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CCriteria_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CCriteria_Point", eInfo.Point);
            dbCmd.Parameters.AddWithValue("@CCriteria_Comments", eInfo.Comments);
            dbCmd.Parameters.AddWithValue("@CCriteria_Ordinal", eInfo.Ordinal);
            dbCmd.Parameters.AddWithValue("@CCriteria_RecruitmentID", eInfo.RecruitmentID);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CCriteria_Update(CCriteriaInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCriteria_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CCriteria_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CCriteria_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CCriteria_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CCriteria_Point", eInfo.Point);
            dbCmd.Parameters.AddWithValue("@CCriteria_Comments", eInfo.Comments);
            dbCmd.Parameters.AddWithValue("@CCriteria_Ordinal", eInfo.Ordinal);
            dbCmd.Parameters.AddWithValue("@CCriteria_RecruitmentID", eInfo.RecruitmentID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
